// Bristleback plugin - Copyright (c) 2010 bristleback.googlecode.com
// ---------------------------------------------------------------------------
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
// This library is distributed in the hope that it will be useful,
// but without any warranty; without even the implied warranty of merchantability
// or fitness for a particular purpose.
// You should have received a copy of the GNU Lesser General Public License along
// with this program; if not, see <http://www.gnu.org/licenses/lgpl.html>.
// ---------------------------------------------------------------------------
package pl.bristleback.server.bristle.messages;

import org.apache.log4j.Logger;
import org.jwebsocket.api.WebSocketConnector;
import org.jwebsocket.token.Token;
import pl.bristleback.server.bristle.exceptions.BristleRuntimeException;

import java.util.Collections;
import java.util.List;

/**
 * Websocket message wrapper, containing token and one or more target connectors.
 * <p/>
 * Created on: 2010-09-21 20:59:28 <br/>
 *
 * @author Wojciech Niemiec
 */
public class WebSocketMessage {
  private static Logger log = Logger.getLogger(WebSocketMessage.class.getName());

  private Token token;
  private List<WebSocketConnector> broadcastConnectors;

  public WebSocketMessage(Token token, WebSocketConnector connector) {
    setToken(token);
    broadcastConnectors = Collections.singletonList(connector);
  }

  private void setToken(Token token) {
    if (token == null) {
      throw new BristleRuntimeException("Websocket message is not complete: missing token.");
    }
    this.token = token;
  }

  public WebSocketMessage(Token token, List<WebSocketConnector> broadcastConnectors) {
    setToken(token);
    this.broadcastConnectors = broadcastConnectors;
  }

  public boolean isBroadcastMessage() {
    return (broadcastConnectors != null) && (broadcastConnectors.size() > 1);
  }

  public Token getToken() {
    return token;
  }

  public List<WebSocketConnector> getBroadcastConnectors() {
    return broadcastConnectors;
  }
}
